package org.example.likou150.tree;

import java.util.*;

// https://leetcode.cn/problems/binary-tree-right-side-view/?envType=study-plan-v2&envId=top-interview-150
public class RightSideView {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> rightSides = new ArrayList<>();
        if (root == null) {
            return rightSides;
        }
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            int queSize = queue.size();
            for (int i = 0; i < queSize; i++) {
                TreeNode treeNode = queue.poll();
                System.out.println(treeNode.val);
                if (treeNode.left != null) queue.offer(treeNode.left);
                if (treeNode.right != null) queue.offer(treeNode.right);

                if (i == queSize - 1) {
                    rightSides.add(treeNode.val);
                }
            }
        }
        return rightSides;
    }


    private class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
 }
}
